'use strict';

const Controller = require('egg').Controller;

const MockData = require('../../mock')

const mysql = require('mysql')

const jwt = require('jsonwebtoken')

class HomeController extends Controller {
  //把数据推送到数据库
  async index() {
    const { ctx, app } = this;
    await MockData.forEach(item => {
      app.mysql.insert('doctor', { title: item.title, name: item.name, img: item.img, num: item.num, price: item.price, introduce: item.introduce })

      ctx.body = {
        code: 200,
        msg: '登录成功'
      }
    })
  }

  //登录
  async login() {
    const { ctx, app } = this
    const { username, password } = ctx.request.body
    const data = await app.mysql.get('logins', { username, password })
    const token = jwt.sign(username, 'xhs')
    if (data) {
      ctx.body = {
        code: 1,
        token,
        data,
        msg: '登录成功'
      }
    } else {
      ctx.body = {
        code: 0,
        msg: '登录失败'
      }
    }
  }

  //注册
  async register() {
    const { ctx, app } = this
    const { username, password } = ctx.request.body
    const data = await app.mysql.get('logins', { username })
    if (data) {
      ctx.body = {
        code: 2,
        msg: '账户已存在'
      }
    } else {
      await app.mysql.insert('logins', { username, password })
      ctx.body = {
        code: 3,
        msg: '注册成功'
      }
    }
  }

  //渲染热点
  async getList() {
    const { ctx, app } = this
    const data = await app.mysql.select('doctor')

    ctx.body = {
      code: 200,
      data
    }
  }

  //渲染睡眠
  async shuiList() {
    const { ctx, app } = this
    const data = await app.mysql.select('睡眠')

    ctx.body = {
      code: 200,
      data
    }
  }

  //渲染生活
  async shengList() {
    const { ctx, app } = this
    const data = await app.mysql.select('生活')

    ctx.body = {
      code: 200,
      data
    }
  }

  //渲染话题
  async huaList() {
    const { ctx, app } = this
    const data = await app.mysql.select('话题')

    ctx.body = {
      code: 200,
      data
    }
  }

  //渲染男性
  async nanList() {
    const { ctx, app } = this
    const data = await app.mysql.select('男性')

    ctx.body = {
      code: 200,
      data
    }
  }

  //热点搜索introduce
  async search() {
    const { ctx, app } = this
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from doctor where introduce like '%${value}%'`)

    ctx.body = {
      code: 200,
      data
    }
  }

  //商城搜索
  async shopSearch() {
    const { ctx, app } = this
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from shop where name like '%${value}%'`)

    ctx.body = {
      code: 200,
      data
    }
  }

  //首页搜索
  async homeSearch() {
    const { ctx, app } = this
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from doctor where name like '%${value}%'`)

    ctx.body = {
      code: 200,
      data
    }
  }

  //话题搜索title
  async search1() {
    const { ctx, app } = this
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from 商品列表 where title like '%${value}%'`)

    ctx.body = {
      code: 200,
      data
    }
  }

  //渲染商城 美洽心选
  async xinxuan() {
    const { ctx, app } = this
    const data = await app.mysql.select('shop')

    ctx.body = {
      code: 200,
      data
    }
  }

  //渲染美洽好评
  async haoping() {
    const { ctx, app } = this
    const data = await app.mysql.select('doctor')

    ctx.body = {
      code: 200,
      data
    }
  }

  //商品列表详情
  async detail() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    const data = await app.mysql.get('商品列表', { id })

    ctx.body = {
      code: 200,
      data
    }
  }

  //渲染商品列表
  async giftList() {
    const { ctx, app } = this
    const data = await app.mysql.select('商品列表')

    ctx.body = {
      code: 200,
      data
    }
  }

  //添加购物车--商品详情
  async addShopCar() {
    const { ctx, app } = this
    const { id, name, price, img, introduce, jia } = ctx.request.body
    const data = await app.mysql.get('shop', { id })

    if (data) {
      const data = await app.mysql.insert('购物车', { jia, name, price, img, introduce })

      ctx.body = {
        code: 200,
        msg: '添加成功',
        data
      }
    } else {
      ctx.body = {
        code: 500,
        msg: '添加失败'
      }
    }
  }

  //添加购物车--列表详情
  async addShopCars() {
    const { ctx, app } = this
    const { id, jia, name, price, img, introduce } = ctx.request.body
    const data = await app.mysql.get('商品列表', { id })

    if (data) {
      const data = await app.mysql.insert('购物车', { jia, name, price, img, introduce })

      ctx.body = {
        code: 200,
        msg: '添加成功',
        data
      }
    } else {
      ctx.body = {
        code: 500,
        msg: '添加失败'
      }
    }
  }

  //购物车渲染
  async shopcar() {
    const { ctx, app } = this
    const data = await app.mysql.select('购物车')

    ctx.body = {
      code: 200,
      data
    }
  }

  //购物车删除
  async shopcardel() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    // console.log(ctx.request.body)
    let data = await app.mysql.delete('购物车', { id })

    ctx.body = {
      code: 200,
      msg: '删除成功',
      data
    }
  }

  //热点详情
  async rddetail() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    const data = await app.mysql.get('doctor', { id })

    ctx.body = {
      code: 200,
      data
    }
  }

  //话题筛选
  async shai() {
    const { ctx, app } = this
    const { val } = ctx.request.body;
    const data = await app.mysql.query(`select * from 话题 where xuan like '%${val}%' `)
    ctx.body = {
      code: 200,
      msg: "搜索成功",
      data
    }
  }

  //购物车+
  async carjia() {
    const { ctx, app } = this
    const { jia, id } = ctx.request.body
    await app.mysql.update('购物车', { jia, id })

    ctx.body = {
      code: 200
    }
  }

  //购物车-
  async carjian() {
    const { ctx, app } = this
    const { jia, id } = ctx.request.body
    await app.mysql.update('购物车', { jia, id })

    ctx.body = {
      code: 200
    }
  }

}

module.exports = HomeController;
